package com.deryun.tyl.controller;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.deryun.tyl.bean.IndexImage;
import com.deryun.tyl.service.impl.IndexImageServiceImpl;

@Controller
@RequestMapping("indexImage")
public class IndexImageController 
{
	@Resource(name="indexImageServiceImpl")
	private IndexImageServiceImpl indexImageSI;
	
	@RequestMapping("setIndexImage")
	public String setIndexImage(
			@RequestParam(value = "imgOne", required = false) MultipartFile imgOne,
			@RequestParam(value = "imgTwo", required = false) MultipartFile imgTwo,
			@RequestParam(value = "imgThree", required = false) MultipartFile imgThree,
			
			@RequestParam(value = "titleOne", required = false) String titleOne,
			@RequestParam(value = "titleTwo", required = false) String titleTwo,
			@RequestParam(value = "titleThree", required = false) String titleThree,
			
			@RequestParam(value = "urlOne", required = false) String urlOne,
			@RequestParam(value = "urlTwo", required = false) String urlTwo,
			@RequestParam(value = "urlThree", required = false) String urlThree,
			
			@RequestParam(value = "plate", required = false) String plate,
			
			HttpServletRequest request
			
			) 
	{
		try 
		{
			String path = "/virtualhost/jbwz/ROOT/upload/image";  
			System.out.println("*************"+path);
			if (!imgOne.isEmpty()) 
			{
				 // 自定义的文件名称
				String editImgOneName= String.valueOf(System.currentTimeMillis()) + ".png";
				File imgOneFile = new File(path, editImgOneName);  
				if(!imgOneFile.exists())
				{  
					imgOneFile.mkdirs();  
				}  
				imgOne.transferTo(imgOneFile);
				// 1
				IndexImage image = new IndexImage();
				image.setPicName("upload/image/"+imgOneFile.getName());
				image.setPicPosition("1");
				image.setPlate(plate);
				image.setTitle(titleOne);
				image.setAddress(urlOne);
				indexImageSI.updateIndex(image);
			}
			if (!imgTwo.isEmpty()) 
			{
				 // 自定义的文件名称
				String editImgTwoName= String.valueOf(System.currentTimeMillis()) + ".png";
				File imgTwoFile = new File(path, editImgTwoName);  
				if(!imgTwoFile.exists())
				{  
					imgTwoFile.mkdirs();  
				}  
				imgTwo.transferTo(imgTwoFile);
					//2
				IndexImage image = new IndexImage();
				image.setPicName("upload/image/"+imgTwoFile.getName());
				image.setPicPosition("2");
				image.setPlate(plate);
				image.setTitle(titleTwo);
				image.setAddress(urlTwo);
				indexImageSI.updateIndex(image);
			}
			if (!imgThree.isEmpty()) 
			{
				 // 自定义的文件名称
				String editImgThreeName= String.valueOf(System.currentTimeMillis()) + ".png";
				File imgThreeFile = new File(path, editImgThreeName);  
				if(!imgThreeFile.exists())
				{  
					imgThreeFile.mkdirs();  
				}  
				imgThree.transferTo(imgThreeFile);
				//3
				IndexImage image = new IndexImage();
				image.setPicName("upload/image/"+imgThreeFile.getName());
				image.setPicPosition("3");
				image.setPlate(plate);
				image.setTitle(titleThree);
				image.setAddress(urlThree);
				indexImageSI.updateIndex(image);
			}
			request.setAttribute("result", "success");
			return "back_admin_manger/index_image";
		} 
		catch (Exception e) 
		{
			request.setAttribute("result", "failed");
			 System.out.println(e.getMessage());
			return "back_admin_manger/index_image";
		}  
	}
}
